/**
 * 测试集合类的序列化
 */
package com.tjh.hadoop.writable;

import java.io.DataOutputStream;
import java.io.FileOutputStream;

import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.junit.Test;

/**
 * @author jahen
 * @date 2016年9月15日
 * @time 下午3:21:41
 * @category Hadoop 序列化
 */
public class TestCollWrtiable {

	/**
	 * 测试ArrayWritable
	 */
	@Test
	public void testArrayWritable() throws Exception{
		//创建writable对象数组
		Writable[] data = new Text[10];
		for(int i = 1 ; i <= 10 ;  i++){
			data[i - 1] = new Text("tom" + i);
		}
		
		//构造成ArrayWritable
		ArrayWritable arr = new ArrayWritable(Text.class);
		arr.set(data);
		arr.write(new DataOutputStream(new FileOutputStream("f:/test2/arr.data")));
	}
	
	/**
	 * 测试MapWritable
	 */
	@Test
	public void testMapWritable() throws Exception{
		MapWritable map = new MapWritable();
		for(int i = 0 ; i < 10 ; i ++){
			map.put(new IntWritable(i), new Text("tom" + i));
		}
		map.write(new DataOutputStream(new FileOutputStream("f:/test2/map.data")));
	}

}
